package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/request")
public class RequesetServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("RequestServlet 接收到请求");


        /*
          uri (uniform resource identifier) 统一资源标识符
            uri = scheme + host + port + path
          url (uniform resource locator) 统一资源定位符
            url = scheme + host + port + path + query
         */
        System.out.println("--------- 请求行内容 -------------");
        // 获取请求行信息
        System.out.println("请求方式：" + request.getMethod());
        System.out.println("uri请求资源路径：" + request.getRequestURI());
        System.out.println("url请求资源路径：" + request.getRequestURL());
        System.out.println("请求协议：" + request.getProtocol());

        System.out.println(request.getMethod() + " " + request.getRequestURI() + " " + request.getProtocol());

        System.out.println("--------- 请求头内容 -------------");
        // 获取请求头信息
        Enumeration<String> headerNames = request.getHeaderNames();

        // 遍历
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            // 获取请求头的值
            String value = request.getHeader(name);
            System.out.println(name + " : " + value);
        }

        // 获取请求 ip 地址 以及 端口号
        String remoteAddr = request.getRemoteAddr();
        int remotePort = request.getRemotePort();
        System.out.println("请求 ip 地址：" + remoteAddr);
        System.out.println("请求端口号：" + remotePort);
    }
}
